package com.lzt.admin.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzt.admin.common.ResultJson;
import com.lzt.admin.common.vo.UserDetailsVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class PrintResponseUtil {
    private final ObjectMapper objectMapper;
    private void print(HttpServletResponse response, ResultJson resultJson) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter writer = response.getWriter();
        String json = objectMapper.writeValueAsString(resultJson);
        writer.write(json);
        response.flushBuffer();
    }
    public void printSuccess(HttpServletRequest request, HttpServletResponse response, UserDetailsVO user) throws IOException {
        Map<String, String> map = new HashMap<>();
//        map.put("username", user.getUsername());
//        map.put("uuid", user.getUser().getUuid());
//        map.put("admin",user.getUser().getAdmin().toString());
//        String orgName = user.getUser().getOrgName();
//        if(!Objects.isNull(orgName)){
//            map.put("orgName", orgName);
//        }
//        String token = jwt.createToken(map);
        print(response,ResultJson.success());
//        print(response, ResultJson.success().add("user",user).add("token",token));
    }
}
